package com.zwz.parse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.zwz.resp.Creator;
import com.zwz.resp.Field;
import com.zwz.resp.Lead;
import com.zwz.resp.ResponseData;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class JsonParser {
    public static List<String> parse(String json) {
//        String json = "..."; // 你的JSON字符串
        ArrayList  arrayList=new ArrayList();
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        try {
            ResponseData responseData = mapper.readValue(json, ResponseData.class);

            // 提取数据示例
           log.info("Stat: " + responseData.getStat());
           log.info("Total Leads: " + responseData.getTotal());


            // 遍历fields
            for (Field field : responseData.getFields()) {
               log.info("Field: " + field.getViewName() + ", Value: " + field.getValue());
            }

            // 遍历leads
            for (Lead lead : responseData.getLeads()) {
               log.info("Enterprise Name: " + lead.getEnterpriseName());
               log.info("Contact: " + lead.getContact());
               log.info("enter_id"+lead.getEnterpriseId());

                // 访问更深层的数据，如creator
                Creator creator = lead.getCreator();
                if (creator != null) {
                   log.info("Creator Name: " + creator.getName());
                }
                arrayList.add(lead.getEnterpriseId());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }



        //放到这里解析


        return arrayList;
    }

    public static int  parseCount(String json) {
//        String json = "..."; // 你的JSON字符串
        ArrayList  arrayList=new ArrayList();
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        int count=0;

        try {
            ResponseData responseData = mapper.readValue(json, ResponseData.class);

            // 提取数据示例
            log.info("Stat: " + responseData.getStat());
            log.info("Total Leads: " + responseData.getTotal());
            count= responseData.getTotal();

            // 遍历fields
            for (Field field : responseData.getFields()) {
                log.info("Field: " + field.getViewName() + ", Value: " + field.getValue());
            }

            // 遍历leads
            for (Lead lead : responseData.getLeads()) {
                log.info("Enterprise Name: " + lead.getEnterpriseName());
                log.info("Contact: " + lead.getContact());
                log.info("enter_id"+lead.getEnterpriseId());

                // 访问更深层的数据，如creator
                Creator creator = lead.getCreator();
                if (creator != null) {
                    log.info("Creator Name: " + creator.getName());
                }
                arrayList.add(lead.getEnterpriseId());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }



        //放到这里解析


        return  count;
    }



}